package com.cmo.rzl.shine.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmo.rzl.core.jfinal.ext.kit.JsonKit;
import com.cmo.rzl.core.toolbox.Record;
import com.cmo.rzl.shine.model.Doctor;
import com.cmo.rzl.shine.service.DoctorService;
import com.cmo.rzl.shine.service.impl.DoctorServiceImpl;
import com.cmo.rzl.system.controller.base.UrlPermissController;
import com.jfinal.kit.StrKit;
import com.mysql.fabric.xmlrpc.base.Data;

/**
 * Generated by Blade.
 * 2017-08-03 10:50:24
 */
public class DoctorController extends UrlPermissController {
	private static String CODE = "doctor";
	private static String PERFIX = "doctor";
	private static String LIST_SOURCE = "Doctor.list";
	private static String BASE_PATH = "/platform/doctor/";
	
	DoctorService service = new DoctorServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "doctor.html");
	}

	public void add() {
		setAttr("code", CODE);
		//查询所有队列
		//\u67E5\u8BE2\u6240\u6709\u961F\u5217
		List<Record> list_queueTypeName= service.queryQueueTypeName();
		//保存list
		setAttr("list_queueTypeName", list_queueTypeName);
		render(BASE_PATH + "doctor_add.html");
	}

	public void edit() {
		setAttr("code", CODE);
		//查询所有队列
		//\u67E5\u8BE2\u6240\u6709\u961F\u5217
		List<Record> list_queueTypeName= service.queryQueueTypeName();
		//保存list
		setAttr("list_queueTypeName", list_queueTypeName);
		String id = getPara(0);
		Doctor doctor = service.findById(id);
		setAttr("model", JsonKit.toJson(doctor));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Doctor doctor = service.findById(id);
		setAttr("model", JsonKit.toJson(doctor));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "doctor_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	public void save() {
		Doctor doctor = mapping(PERFIX, Doctor.class);
		// 根据医生登陆工号查询
		//\u6839\u636E\u533B\u751F\u767B\u9646\u5DE5\u53F7\u67E5\u8BE2
		Doctor doctor_login_id = service.findByLogin_id(doctor.getLogin_id());
		if (null != doctor_login_id && doctor_login_id.getDoctor_id() != null) {
			renderJson(error(DOCTOR_LOGIN_ID_MSG));
			return;
		}
		doctor.setOpr_time( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss").format(new Date()));
		boolean temp = service.save(doctor);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Doctor doctor = mapping(PERFIX, Doctor.class);
		doctor.setOpr_time( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss").format(new Date()));
		//boolean temp = service.update(doctor);
		boolean temp=service.updateEveryCol(doctor);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}
	
	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
	
	public void save_doctor() {
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取入参
		String doctor_strs = getPara("doctors");
		if (StrKit.isBlank(doctor_strs)) {
			map.put("code", -1);
			map.put("msg", "请传值");
			renderJson(map);
			return;
			
			
		} else {
			JSONArray doctorList = JSONArray.parseArray(doctor_strs);
			if (doctorList != null) {
				Doctor doctor = null;
				for (Object json : doctorList) {
					JSONObject doctor_json = (JSONObject) json;
					doctor = new Doctor();
					// 根据医生登陆工号查询
					Doctor doctor_login_id = service.findByLogin_id(doctor_json
							.getString("login_id"));
					if (null != doctor_login_id
							&& doctor_login_id.getDoctor_id() != null) {
						// 修改医生信息
						boolean bool = service.updateBy(
								"name='" + doctor_json.getString("name")
										+ "',department='"
										+ doctor_json.getString("department")
										+ "',title='"
										+ doctor_json.getString("title") + "'",
								" where login_id in('"
										+ doctor_json.getString("login_id")
										+ "')", null);
						if (bool) {
							map.put("code", 200);
							map.put("msg", "医生信息修改成功");
							renderJson(map);
						}
					} else {
						// 设置医生工号值
						doctor.setLogin_id(doctor_json.getString("login_id"));
						// 设置医生姓名
						doctor.setName(doctor_json.getString("name"));
						// 设置医生职称
						doctor.setTitle(doctor_json.getString("title"));
						// 设置医生简介
						doctor.setDescription(doctor_json
								.getString("description"));
						// 保存
						boolean bool = service.save(doctor);
						if (bool) {
							map.put("code", 200);
							map.put("msg", "成功");
							renderJson(map);
						} else {
							map.put("code", -1);
							map.put("msg", "失败");
							renderJson(map);
							return;
						}
					}
				}
			}
		}
		renderJson(map);
	}
}
